package com.magenta.maxoptra.gis;


/*
* Project: MaxOptRA v1.2
* User: Ivan Usalko
* Date: 13.09.2009 17:03:39
*
* Copyright (c) 1999-2009 Magenta Corporation Ltd. All Rights Reserved.
* Magenta Technology proprietary and confidential.
* Use is subject to license terms.
*/
public class ResponseRouting extends ResponseGeoInfo {
    static final long serialVersionUID = 745681756822540355L;

    /**
     * requestID...
     */
    transient final GeoInfoRequestId requestId;

    /**
     * Distance in miles...
     */
    final double distance;

    /**
     * Duration in millis.
     */
    final long duration;

    /**
     * Placed time into cache in nanoseconds.
     */
    transient Long placedTime;

    /**
     * Debug time points.
     */
    transient Long beforeRequestTime;
    transient Long beforeQueuePlaced;

    /**
     * From and to landmarks longitudes and latitudes.
     * Spherical coords on surface.
     * Measured in degrees.
     */
    transient Double latFrom;
    transient Double lonFrom;
    transient Double latTo;
    transient Double lonTo;

    public ResponseRouting(GeoInfoRequestId requestId, double distance, long duration, Long placedTime) {
        this.requestId = requestId;
        this.distance = distance;
        this.duration = duration;
        this.placedTime = placedTime;
    }

    public GeoInfoRequestId getRequestId() {
        return requestId;
    }

    @Override
    public Long getPlacedTime() {
        return placedTime;
    }

    public double getDistance() {
        return distance;
    }

    @Override
    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append(ResponseRouting.class.getSimpleName()).append("[");
        buf.append(String.format("distance : %f miles", distance)).append(", ");
        buf.append(String.format("duration : %d millisec", duration));
        buf.append("]");
        return buf.toString();
    }

    public long getDuration() {
        return duration;
    }


    private static boolean enableTimestamps = Boolean.valueOf(System.getProperty("enable.memcached-timestamps", "false"));
    /**
     * Parametrical serialization in memcached.
     * @param s object input stream.
     * @throws java.io.IOException throw if problem in io operations in input stream.
     * @throws ClassNotFoundException throw if class not loaded.
     */
    private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();
        if (enableTimestamps) {
            placedTime = (Long) s.readObject();
            beforeRequestTime = (Long) s.readObject();
            beforeQueuePlaced = (Long) s.readObject();
        }
    }

    /**
     * Save the <tt>RouteGeoInfo</tt> instance to a stream (that
     * is, serialize it).
     * @param s output stream.
     * @throws java.io.IOException if write not possible.
     */
    private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException{
        s.defaultWriteObject();
        if (enableTimestamps) {
            s.writeObject(placedTime);
            s.writeObject(beforeRequestTime);
            s.writeObject(beforeQueuePlaced);
        }
    }

    public void setBeforeRequestTime(Long beforeRequestTime) {
        this.beforeRequestTime = beforeRequestTime;
    }

    public void setBeforeQueuePlaced(Long beforeQueuePlaced) {
        this.beforeQueuePlaced = beforeQueuePlaced;
    }

    public Long getBeforeRequestTime() {
        return beforeRequestTime;
    }

    public Long getBeforeQueuePlaced() {
        return beforeQueuePlaced;
    }

    public Double getLatitudeFrom() {
        return latFrom;
    }

    public void setLatitudeFrom(Double latFrom) {
        this.latFrom = latFrom;
    }

    public Double getLongitudeFrom() {
        return lonFrom;
    }

    public void setLongitudeFrom(Double lonFrom) {
        this.lonFrom = lonFrom;
    }

    public Double getLatitudeTo() {
        return latTo;
    }

    public void setLatitudeTo(Double latTo) {
        this.latTo = latTo;
    }

    public Double getLongitudeTo() {
        return lonTo;
    }

    public void setLongitudeTo(Double lonTo) {
        this.lonTo = lonTo;
    }

    public boolean withoutNullCoords() {
        return latFrom != null && lonFrom != null && latTo != null && lonTo != null;
    }
}
